package com.ww.learn.oop.sam

/**
 * 数据过滤
 */
fun interface Filter<T> {
    fun shouldInclude(item: T): Boolean
}

class DataProcessor<T> {
    fun filterData(data: List<T>, filter: Filter<T>): List<T> {
        return data.filter { filter.shouldInclude(it) }
    }
}

// 打印结果
//[Alice, Charlie, David]
//[Alice]
fun main() {
    val processor = DataProcessor<String>()
    val names = listOf("Alice", "Bob", "Charlie", "David", "Eve")

    // 过滤长度大于3的名字
    val longNames = processor.filterData(names) { it.length > 3 }
    println(longNames)

    // 过滤以A开头的名字
    val aNames = processor.filterData(names) { it.startsWith("A") }
    println(aNames)
}